<?php 
  require_once('conn.php');  
  $action = isset($_GET["act"]) ? $_GET["act"] : "";
  if($action=="save"){
	  $tid=$_POST["tid"];
	  $ps=$_POST["ps"];
	  $question=$_POST["question"];
	  $imgurl=$_POST["imgurl"];
	  $linkurl=$_POST["linkurl"];
	  $jieshao=$_POST["jieshao"];
	 if(empty($tid) || !is_numeric($tid)){
		 mysql_close($conn);
		 errormsg("标题参数不能为空且只能是数字!");
		 exit;
	 }
	 if(!is_numeric($ps) && intval($ps)>=0){
		 mysql_close($conn);
		 errormsg("票数只能为正整数!");
		 exit();
	 }
	 if($question==""){
		 mysql_close($conn);
		 errormsg("投票问题不能为空!");
		 exit();
	 }else{
		 $qry = mysql_query("select * from xh_question where question='".$question."' and tid=".$tid,$conn);
		 if(mysql_num_rows($qry)>0){
			 mysql_free_result($qry);
			 mysql_close($conn);
			 errormsg("在同一标题该投票问题已经存在!");			 
			 exit();
		 }
		 mysql_free_result($qry);		 
	 }	 
	 $qry = mysql_query("select * from xh_title where id=".$tid,$conn);
     $sid="";
	 $ms="";
	 if($row=mysql_fetch_array($qry)){
		 $sid=$row["sid"];
		 $ms=$row["ms"];
	 }
	 if(empty($sid)){
		 mysql_free_result($qry);
		 mysql_close($conn);
		 errormsg("主题缺失,该记录不能添加!");		 
		 exit();
	 }
//	 $cmd="insert into xh_question(question,imgurl,linkurl,sid,tid,ps) values('".$question."','".$imgurl."','".$linkurl."',".$sid.",".$tid.",".$ps.")";
	 $cmd="insert into xh_question(question,imgurl,linkurl,jieshao,sid,tid,ps) values('".$question."','".$imgurl."','".$linkurl."','".$jieshao."',".$sid.",".$tid.",".$ps.")";
	 $ins = mysql_query($cmd,$conn);
	 if($ms=="1"){
		 $update = mysql_query("update xh_title set vcount=vcount+".$ps." where id=".$tid,$conn);
	 }else if($ms=="2"){
		 $update = mysql_query("update xh_title set vcount=vcount+1 where id=".$tid,$conn);
	 }
	 mysql_free_result($qry);
	 mysql_close($conn);
	 successmsg("添加投票问题成功!");
	 exit();
 }
?>
<html>
<head>
<?php
  head();
  check();
?>
<title>后台管理中心</title>
<script language="javascript" type="text/javascript">
function isnum(sText)
{
	var validChars="0123456789";
	var isnumber = true;
	for(i=0;(i<sText.length) &&(isnumber);i++)
	{
		ch = sText.charAt(i);
		if(validChars.indexOf(ch)==-1)
		{
			isnumber=false;
		}
	}
	return isnumber;
}
function check()
{	
	if(form3.tid.selectedIndex==-1)
	{
		document.getElementById("lbltid").innerHTML = "<font color='red'>投票问题不能为空</font>";
		form3.tid.focus();
		return false;
	}
	else
	{
		document.getElementById("lbltid").innerHTML = "";
	}
	if(form3.question.value=="")
	{
		
		document.getElementById("lblquestion").innerHTML = "<font color='red'>投票答案名称不能为空</font>";
		form3.question.focus();
		return false;
	}
	else
	{
		document.getElementById("lblquestion").innerHTML = "";
	}
	if(form3.ps.value=="")
	{
		document.getElementById("lblps").innerHTML = "<font color='red'>票数不能为空</font>";
		form3.ps.focus();
		return false;
	}
	else
	{
		if(!isnum(form3.ps.value))
		{
			document.getElementById("lblps").innerHTML = "<font color='red'>票数只能是数字</font>";
			form3.ps.focus();
			return false;
		}
		else
		{
			document.getElementById("lblps").innerHTML = "";
		}
	}
	return true;
}
var xmlHttp = null;
function createXMLHttpRequest(){
	if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		xmlHttp = new XMLHttpRequest();
	}
}
function ajax(){
	params = document.getElementById("linkurl").value;
	params.replace(/(^\s*)|(\s*$)/g,"");
	if(params==""){
		alert("请输入链接地址!");
		return;
	}
	createXMLHttpRequest();
	if(xmlHttp==null){
		alert("请使用IE或FireFox浏览器!");
		return ;
	}
	document.getElementById("lbllinkurl").innerHTML="正在检查,请等待...";
	xmlHttp.onreadystatechange=callback;
	xmlHttp.open("GET","validate.php?linkurl="+params);
	xmlHttp.send(null);
}
function callback(){
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){
			document.getElementById("lbllinkurl").innerHTML=xmlHttp.responseText;
		}
	}
}
</script>
</head>
<body>
<table border="0" cellpadding="1" cellspacing="2" width="100%" id="table1" bgcolor="#E1F0FF">
  <tr>
    <td><table width="100%" id="table2" cellspacing="1">
        <tr>
          <td id="TableTitle1"> 添加投票答案-&gt;</td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td><table border="1" cellpadding="4" cellspacing="0" width="100%" id="table2" bordercolor="#BFDFFF">
        <form action="?act=save" name="form1" id="form1" method="post" onSubmit="return check();">
          <tr id="TableTitle2">
            <td align="center" colspan="2" bgcolor="#BFDFFF"><b>设置投票答案</b></td>
          </tr>
          <tr>
            <td align="right">选择投票问题：</td>
            <td><select name="tid" id="tid">
              <?php			   
			   $sqry = mysql_query("select * from xh_subject order by id asc",$conn);
			   while($srow=mysql_fetch_array($sqry))
			   {
				   echo "<optgroup label='".$srow["subject"]."'>";
				   $tqry = mysql_query("select * from xh_title where sid=".$srow["id"]." order by id asc",$conn);
				   while($trow=mysql_fetch_array($tqry))
				   {
					   echo "<option value='".$trow["id"]."'>".$trow["title"]."</option>";
				   }
				   mysql_free_result($tqry);
				   echo "</optgroup>";
			   }
			   mysql_free_result($sqry);			   
			  ?>
              </select>
			  <label id="lbltid" name="lbltid"></label>
            </td>
          </tr>
          <tr>
            <td width="20%" align="right">投票答案名称：</td>
            <td><input type="text" name="question" id="question" size="25"><label id="lblquestion" name="lblquestion"></label>
            </td>
          </tr>
          <tr>
            <td width="20%" align="right">图片地址：</td>
            <td><input type="text" name="imgurl" id="imgurl" size="30">&nbsp;无需填写 上传文件后自动填入地址&nbsp;<label id="error" name="error"></label>
            </td>
          </tr>
          <tr>
            <td width="20%" align="right">上传图片：</td>
            <td><iframe src="upload.php" frameborder="0" height="25" width="380" scrolling="no"></iframe>图片不能太大
            </td>
          </tr>
          <tr>
            <td width="20%" align="right">链接地址：</td>
            <td><input type="text" name="linkurl" id="linkurl" size="30">&nbsp;<a href="javascript:void(0)" onclick="ajax();">有效链接地址检查</a>&nbsp;<label id="lbllinkurl" name="lbllinkurl"></label>
            </td>
          </tr>
		  <tr>
            <td width="20%" align="right">描述：</td>
            <td><input type="text" name="jieshao" id="jieshao"  size="30"><label id="jieshao" name="jieshao"></label>
            </td>
          </tr>
          <tr>
            <td width="20%" align="right">票数：</td>
            <td><input type="text" name="ps" id="ps" size="5" value="0"><label id="lblps" name="lblps"></label></td>
          </tr>
          <tr>
            <td colspan="2" align="center"><input class="btn" type="submit" name="submit" value=" 添 加 "></td>
          </tr>
        </form>
      </table></td>
  </tr>
</table>
</body>
</html>
<?php mysql_close($conn);?>